package org.example;

/**
 * @className: MyListNode
 * @author: 芃
 * @description:
 * @date: 2024/5/17 9:45
 */
public class MyListNode {
    class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=node5;

        this.head=node1;
    }

    public int size(){
        ListNode cur=head;
        int count=0;

        while(cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }

    public void dispaly(){
        ListNode cur=head;

        while (cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }

    public void  reverseList(){
        //简洁的方法

        //考虑头节点为null的情况
        if(head==null){
            return;
        }
        ListNode cur=head;
        while (cur!=null){
            ListNode curN=cur.next;
            cur.next=head;//cur起初本身就是头节点，你让头节点的next指向了头节点本身，头节点这里就已经死循环了
            head=cur;
            cur=curN;
        }



//        ListNode cur=head.next;
//        ListNode curN=head;
//
//        //要考虑头节点的为null
//        if(head==null){
//            return;
//        }
//
//        while (cur!=null){
//            head.next=cur.next;
//            cur.next=curN;
//            curN=cur;
//            cur=head.next;
//        }
//        head.next=null;
//        head=curN;
    }

}
